home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0391B.ZIP / QED.PAS < prev    next >
Pascal/Delphi Source File  |  1987-02-22  |  22KB  |  1,046 lines

  1.  
  2. (*
  3.  * quick editor
  4.  *
  5.  * This is a simple and fast editor to use
  6.  * when you want to quickly change a file.  It is not
  7.  * meant to be used as a programming editor
  8.  *
  9.  *)
  10.  
  11.  program QuickEditor;
  12.  
  13. {$c-,v-,i-}
  14.  
  15.  type
  16.    linestring =        string [128];
  17.    lineptr    =        ^linestring;
  18.  
  19.  
  20.  const
  21.    maxlines =          5000;
  22.  
  23.  
  24.  var
  25.    keynum,
  26.    currentline,
  27.    column,
  28.    i,
  29.    highestline,
  30.    screenline:         integer;
  31.    inkey,
  32.    secinkey,
  33.    choice,
  34.    ch:                 char;
  35.    linebuffer:         array [1.. maxlines] of lineptr;
  36.    emptyline:          lineptr;
  37.    tabset:             array [1..80] of boolean;
  38.    textfile:           text[20480];
  39.    tempword,
  40.    filename,
  41.    typeset:            linestring;
  42.    searchstring,
  43.    replacement:        linestring;
  44.    secnum,
  45.    insertmode:         boolean; 
  46.  
  47. type
  48.    screenloc = record
  49.       character: char;
  50.       attribute: byte;
  51.    end;
  52.  
  53.    videoram = array[1..2000] of screenloc;
  54.    videoptr = ^videoram;
  55.  
  56.  
  57.  
  58.  procedure quick_display(x,y: integer;  s: linestring);
  59.  var
  60.     i:        integer;
  61.     display:  videoram absolute $b800:0;
  62.     index:    integer;
  63.     len:      integer;
  64.  
  65.  begin
  66.  
  67.     index := y*80 + x;
  68.  
  69.     len := length(s);
  70.     if len > 80 then
  71.        len := 80;
  72.  
  73.     for i := 1 to len do
  74.     begin
  75.        display[index].character := s[i];
  76.        index := index + 1;
  77.     end;
  78.  end;
  79.  
  80. procedure drawscreen;
  81. var
  82.    i:  integer;
  83. begin
  84.    clrscr;
  85.  
  86.    for i := 1 to 22 do
  87.       quick_display(1,i,linebuffer [currentline-screenline+i]^);
  88. end;
  89.  
  90.  function replicate (count,
  91.                      ascii:              integer): linestring;
  92.  var
  93.    temp:               linestring;
  94.    i:                  byte;
  95.  
  96.  begin
  97.    temp := '';
  98.  
  99.    for i := 1 to count do
  100.       temp := temp + chr (ascii);
  101.  
  102.    replicate := temp;
  103.  end;
  104.  
  105.  
  106.  procedure newbuffer(var buf: lineptr);
  107.  begin
  108.  
  109.     new(buf);
  110.  
  111.     if (memavail > 0) and (memavail < 200) then
  112.     begin
  113.        window(1, 1, 80, 25);
  114.        gotoxy(75,1);
  115.        write(memavail:5);
  116.        window(1, 2, 80, 23);
  117.        gotoxy(1,1);
  118.  
  119.        sound(700);
  120.        delay(500);
  121.        sound(1300);
  122.        delay(600);
  123.        nosound;
  124.     end;
  125.  end;
  126.  
  127.  
  128.  procedure loadfile (name:               linestring);
  129.  begin
  130.    window(1, 2, 80, 23);
  131.    clrscr;
  132.    assign(textfile, name);    {$i-}
  133.  
  134.    reset(textfile);           {$i+}
  135.    
  136.  
  137.    if (ioresult <> 0) then
  138.    begin
  139.       clrscr;
  140.       writeln(chr (7));
  141.       writeln('File does not exist: ', name);
  142.       halt;
  143.    end;
  144.  
  145.    writeln;
  146.    write('      Reading ',name);
  147.  
  148.    for i := 1 to maxlines do
  149.    begin
  150.       if (linebuffer[i] <> emptyline) then
  151.          linebuffer[i]^ := emptyline^;
  152.    end;
  153.  
  154.    currentline := 1;
  155.  
  156.    while not eof (textfile) do
  157.    begin
  158.       if (currentline mod 100) = 0 then
  159.          write(#13,currentline);
  160.  
  161.       if linebuffer[currentline] = emptyline then
  162.          newbuffer(linebuffer[currentline]);
  163.  
  164.       readln(textfile, linebuffer [currentline]^);
  165.  
  166.       currentline := currentline + 1;
  167.       if (currentline > maxlines) then
  168.       begin
  169.          writeln('File is too long to edit with QED');
  170.          writeln('Only compiled for ',maxlines,' lines');
  171.          halt;
  172.       end;
  173.    end;
  174.  
  175.    close(textfile);
  176.  
  177.    highestline := currentline + 1;
  178.    currentline := 1;
  179.    column := 1;
  180.    screenline := 1;
  181.    drawscreen;
  182.  
  183.  end;
  184.  
  185.  procedure dispkey (s:                  linestring);
  186.  begin
  187.    normvideo;
  188.    write(s [1]);
  189.    lowvideo;
  190.    write(copy (s, 2, 80));
  191.  end;
  192.  
  193.  procedure displaykeys;
  194.  begin
  195.    window(1, 1, 80, 25);
  196.    gotoxy(1, 25);
  197.    clreol;
  198.    dispkey('1Help  ');
  199.    dispkey('2Locate  ');
  200.    dispkey('3Search  ');
  201.    dispkey('4Replace  ');
  202.    dispkey('5SaveQuit  ');
  203.    dispkey('6InsLine  ');
  204.    dispkey('7DelLine  ');
  205.    dispkey('0QuitNosave  ');
  206.    normvideo;
  207.    window(1, 2, 80, 23);
  208.  end;
  209.  
  210.  procedure initialize;
  211.  begin
  212.  
  213.    if (paramcount <> 1) then
  214.    begin
  215.       writeln('Usage:  qed FILENAME');
  216.       halt;
  217.    end;
  218.  
  219.    clrscr;
  220.    crtinit;
  221.    window(1, 1, 80, 25);
  222.    gotoxy(1,1);
  223.    write(replicate (80, 205));
  224.    gotoxy(1,24);
  225.    write(replicate (80, 196));
  226.    gotoxy(12,1);
  227.    write(' Quick Editor ');
  228.    gotoxy(29,1);
  229.    write(' ',paramstr (1),' ');
  230.    displaykeys;
  231.    currentline := 1;
  232.    column := 1;
  233.    screenline := 1;
  234.    highestline := 1;
  235.    newbuffer(emptyline);
  236.    emptyline^ := '';
  237.    searchstring := '';
  238.    replacement := '';
  239.    insertmode := false;
  240.  
  241.    for i := 1 to 80 do
  242.       tabset[i]:=(i mod 8)= 1;
  243.  
  244.    for i := 1 to maxlines do
  245.       linebuffer[i] := emptyline;
  246.  
  247.    gotoxy(10, 20);
  248.    loadfile(paramstr (1));
  249.  end;
  250.  
  251. procedure help;
  252. begin
  253.    clrscr;
  254.    quick_display(1, 1,'Quick editor commands:');
  255.    quick_display(5, 3,'<BACKSPACE>, <TAB>, <ENTER>, <HOME>, <END>, ');
  256.    quick_display(5, 4,'<PGUP>, <PGDN>, <DELETE>, <arrow keys>');
  257.    quick_display(5, 5,'  - These keys operate as expected');
  258.    quick_display(5, 7,'<ESC>       Erase current line');
  259.    quick_display(5, 8,'<INSERT>    Toggle insert/replace mode');
  260.    quick_display(5, 9,'CTL/LEFT    Previous word');
  261.    quick_display(5,10,'CTL/RIGHT   Next word');
  262.    quick_display(5,11,'CTL/PGUP    Top of file');
  263.    quick_display(5,12,'CTL/PGDN    End of file');
  264.    quick_display(5,13,'F1          Print these instructions');
  265.    quick_display(5,14,'F2          Locate all lines with a string');
  266.    quick_display(5,15,'F3          Search for a string');
  267.    quick_display(5,16,'F4          Global search and replace');
  268.    quick_display(5,17,'F5          Save file and quit');
  269.    quick_display(5,18,'F6          Insert blank line');
  270.    quick_display(5,19,'F7          Delete current line');
  271.    quick_display(5,20,'F10         Abort edit');
  272.  
  273.  
  274.    window(1, 1, 80, 25);
  275.    gotoxy(1, 25);
  276.    clreol;
  277.    write('Press any key to return to editing...');
  278.  
  279.    repeat
  280.    until keypressed;
  281.    read(kbd,ch);
  282.  
  283.    displaykeys;
  284.    drawscreen;
  285. end;
  286.  
  287.  
  288.  procedure printrow;
  289.  begin
  290.    window(1, 1, 80, 25);
  291.    gotoxy(54,1);
  292.    write(' line ', currentline : 4,' ');
  293.    gotoxy(68,1);
  294.    write(' col ', column : 2,' ');
  295.    window(1, 2, 80, 23);
  296.  end;
  297.  
  298.  function getkey (var secnum:         boolean;
  299.                   var inkey:          char): boolean;
  300.  begin
  301.    
  302.    if keypressed then
  303.    begin
  304.       read(kbd,inkey);
  305.       secnum := (inkey = #27) and keypressed;
  306.  
  307.       if secnum then
  308.       begin
  309.          read(kbd,ch);
  310.          keynum := ord (ch) + 128;
  311.       end
  312.       else
  313.  
  314.       if ord (inkey)<= 27 then
  315.       begin
  316.          secnum := true;
  317.          keynum := ord (inkey);
  318.       end
  319.       else
  320.       begin
  321.          keynum := ord (inkey);
  322.          secnum := false;
  323.       end
  324.    end
  325.    else
  326.    begin
  327.       getkey := false;
  328.       secnum := false;
  329.    end;
  330.  end;
  331.  
  332.  procedure character;
  333.  begin
  334.  
  335.    if column = 79 then
  336.    begin
  337.       sound(510);
  338.       delay(30);
  339.       nosound;
  340.    end
  341.    else
  342.  
  343.    begin
  344.       gotoxy(column, screenline);
  345.       write(inkey);
  346.  
  347.       if linebuffer[currentline] = emptyline then
  348.       begin
  349.          newbuffer(linebuffer[currentline]);
  350.          linebuffer[currentline]^ := '';
  351.       end;
  352.  
  353.       while length(linebuffer[currentline]^) < column do
  354.          linebuffer[currentline]^ := linebuffer[currentline]^ + ' ';
  355.  
  356.       insert(inkey, linebuffer [currentline]^, column);
  357.       column := column + 1;
  358.  
  359.       if not insertmode then
  360.          delete(linebuffer [currentline]^, column, 1);
  361.  
  362.  
  363.  
  364. (* redraw current line if in insert mode *)
  365.       if insertmode then
  366.          quick_display(1,screenline,linebuffer [currentline]^);
  367.  
  368.  
  369. (* ding the bell when close to the end of a line *)
  370.  
  371.       if column = 70 then
  372.       begin
  373.          sound(1010);
  374.          delay(10);
  375.          nosound;
  376.       end;
  377.    end;
  378.  end;
  379.  
  380.  procedure beginfile;
  381.  begin
  382.    currentline := 1;
  383.    column := 1;
  384.    screenline := 1;
  385.    drawscreen;
  386.  end;
  387.  
  388.  procedure endfile;
  389.  begin
  390.    currentline := highestline + 1;
  391.    screenline := 12;
  392.    column := 1;
  393.    drawscreen;
  394.  end;
  395.  
  396.  procedure funcend;
  397.  begin
  398.    column := length (linebuffer [currentline]^) + 1;
  399.    if column > 80 then
  400.       column := 80;
  401.  end;
  402.  
  403.  procedure cursorup;
  404.  var
  405.    count:              integer;
  406.  
  407.  begin
  408.  
  409.    if currentline = 1 then
  410.       exit;
  411.  
  412.    currentline := currentline - 1;
  413.    if screenline = 1 then
  414.    begin
  415.       gotoxy(1, 1);
  416.       insline;
  417.       quick_display(1,1,linebuffer [currentline]^);
  418.    end
  419.    else
  420.       screenline := screenline - 1;
  421.  end;
  422.  
  423.  procedure cursordown;
  424.  begin
  425.    currentline := currentline + 1;
  426.    if currentline > highestline then
  427.       highestline := currentline;
  428.  
  429.    screenline := screenline + 1;
  430.    if screenline > 22 then
  431.    begin
  432.       gotoxy(1, 1);
  433.       delline;
  434.       screenline := 22;
  435.       quick_display(1,screenline,linebuffer [currentline]^);
  436.    end;
  437.  end;
  438.  
  439.  
  440.  procedure insertline;
  441.  begin
  442.    insline;
  443.  
  444.    for i := highestline + 1 downto currentline do
  445.       linebuffer[i + 1] := linebuffer [i];
  446.  
  447.    linebuffer[currentline] := emptyline;
  448.    highestline := highestline + 1;
  449.  end;
  450.  
  451.  
  452.  procedure enter;
  453.  begin
  454.    cursordown;
  455.    column := 1;
  456.    gotoxy(column, screenline);
  457.  
  458.    if insertmode then
  459.       insertline;
  460.  end;
  461.  
  462.  
  463.  procedure deleteline;
  464.  begin
  465.    delline;
  466.  
  467.    if highestline > currentline +(23 - screenline) then
  468.       quick_display(1,22,linebuffer [currentline +(23 - screenline)]^);
  469.  
  470.    if linebuffer[currentline] <> emptyline then
  471.       linebuffer[currentline]^ := emptyline^;
  472.  
  473.    for i := currentline to highestline + 1 do
  474.       linebuffer[i] := linebuffer [i + 1];
  475.  
  476.    linebuffer [highestline+2] := emptyline;
  477.    highestline := highestline - 1;
  478.  
  479.    if currentline > highestline then
  480.       highestline := currentline;
  481.  end;
  482.  
  483.  
  484.  procedure cursorleft;
  485.  begin
  486.    column := column - 1;
  487.  
  488.    if column < 1 then
  489.    begin
  490.       cursorup;
  491.       funcend;
  492.    end
  493.  end;
  494.  
  495.  procedure cursorright;
  496.  begin
  497.    column := column + 1;
  498.  
  499.    if column > 79 then
  500.    begin
  501.       cursordown;
  502.       column := 1;
  503.    end;
  504.  end;
  505.  
  506.  procedure ins;
  507.  begin
  508.  
  509.    if insertmode then
  510.       insertmode := false
  511.    else
  512.       insertmode := true;
  513.  
  514.    window(1,1,80,25);
  515.    gotoxy(1,1);
  516.  
  517.    if insertmode then
  518.       write('Insert ')
  519.    else
  520.       write(replicate (7, 205));
  521.  
  522.    window(1,2,80,23);
  523.  end;
  524.  
  525.  procedure del;
  526.  begin
  527.    if (column > length(linebuffer[currentline]^)) then
  528.    begin
  529.       if (length(linebuffer[currentline]^) +
  530.           length(linebuffer[currentline+1]^)) < 80 then
  531.       begin
  532.          linebuffer[currentline]^ := linebuffer[currentline]^ +
  533.                                      linebuffer[currentline+1]^;
  534.  
  535.          quick_display(1,screenline,linebuffer [currentline]^);
  536.          cursordown;
  537.          deleteline;
  538.          cursorup;
  539.       end;
  540.       exit;
  541.    end;
  542.  
  543.  
  544.    if linebuffer[currentline] = emptyline then
  545.    begin
  546.       newbuffer(linebuffer[currentline]);
  547.       linebuffer[currentline]^ := '';
  548.    end;
  549.  
  550.    while length(linebuffer[currentline]^) < column do
  551.       linebuffer[currentline]^ := linebuffer[currentline]^ + ' ';
  552.  
  553.    ch := copy (linebuffer [currentline]^, column, 1);
  554.    delete(linebuffer [currentline]^, column, 1);
  555.  
  556.    gotoxy(1,screenline);
  557.    clreol;
  558.    quick_display(1,screenline,linebuffer [currentline]^)
  559.  end;
  560.  
  561.  procedure backspace;
  562.  begin
  563.  
  564.    if column > 1 then
  565.       column := column - 1
  566.    else
  567.    begin
  568.       cursorup;
  569.       funcend;
  570.       del;
  571.    end;
  572.  end;
  573.  
  574.  
  575.  procedure terminate;
  576.  begin
  577.    window(1, 1, 80, 25);
  578.    gotoxy(1, 25);
  579.    clreol;
  580.    gotoxy(1, 24);
  581.    clreol;
  582.    write('      Writing...');
  583.  
  584.    rewrite(textfile);
  585.    for i := 1 to highestline + 1 do
  586.    begin
  587.       if (i mod 100) = 0 then
  588.          write(#13,i);
  589.  
  590.       writeln(textfile, linebuffer [i]^);
  591.    end;
  592.  
  593.    write(#13,i);
  594.    writeln(textfile,^Z);
  595.    close(textfile);
  596.    write(#13);
  597.    clreol;
  598.    halt;
  599.  end;
  600.  
  601.  procedure quitnosave;
  602.  begin
  603.    window(1, 1, 80, 25);
  604.    gotoxy(1, 25);
  605.    clreol;
  606.    gotoxy(1, 24);
  607.    clreol;
  608.    halt;
  609.  end;
  610.  
  611.  procedure funcpgup;
  612.  begin
  613.    currentline := currentline - 20;
  614.    if currentline <= screenline then 
  615.       beginfile
  616.    else
  617.       drawscreen;
  618.  end;
  619.  
  620.  procedure funcpgdn;
  621.  begin
  622.    currentline := currentline + 20;
  623.    if currentline+12 >= highestline then
  624.       endfile
  625.    else
  626.       drawscreen;
  627.  end;
  628.  
  629.  
  630.  
  631.  procedure prevword;
  632.  begin
  633.  
  634. (* if i am in a word then skip to the space *)
  635.    while (not ((linebuffer[currentline]^[column] = ' ') or
  636.                (column >= length(linebuffer[currentline]^) ))) and
  637.          ((currentline <> 1) or
  638.           (column <> 1)) do
  639.       cursorleft;
  640.  
  641. (* find end of previous word *)
  642.    while ((linebuffer[currentline]^[column] = ' ') or
  643.           (column >= length(linebuffer[currentline]^) )) and
  644.          ((currentline <> 1) or
  645.           (column <> 1)) do
  646.       cursorleft;
  647.  
  648. (* find start of previous word *)
  649.    while (not ((linebuffer[currentline]^[column] = ' ') or
  650.                (column >= length(linebuffer[currentline]^) ))) and
  651.          ((currentline <> 1) or
  652.           (column <> 1)) do
  653.       cursorleft;
  654.  
  655.    cursorright;
  656.  end;
  657.  
  658.  procedure nextword;
  659.  begin
  660.  
  661. (* if i am in a word, then move to the whitespace *)
  662.    while (not ((linebuffer[currentline]^[column] = ' ') or
  663.                (column >= length(linebuffer[currentline]^)))) and
  664.          (currentline < highestline) do
  665.       cursorright;
  666.  
  667. (* skip over the space to the other word *)
  668.    while ((linebuffer[currentline]^[column] = ' ') or
  669.           (column >= length(linebuffer[currentline]^))) and
  670.          (currentline < highestline) do
  671.       cursorright;
  672.  
  673.  end;
  674.  
  675.  procedure tab;
  676.  begin
  677.    
  678.    if column < 79 then
  679.    begin
  680.       
  681.       repeat
  682.          column := column + 1;
  683.       until (tabset [column]= true) or (column = 79);
  684.    end;
  685.  end;
  686.  
  687.  procedure backtab;
  688.  begin
  689.    
  690.    if column > 1 then
  691.    begin
  692.  
  693.       repeat
  694.          column := column - 1;
  695.       until (tabset [column]= true) or (column = 1);
  696.    end;
  697.  end;
  698.  
  699.  procedure esc;
  700.  begin
  701.    column := 1;
  702.    gotoxy(1, wherey);
  703.    clreol;
  704.  
  705.    if (linebuffer[currentline] <> emptyline) then
  706.       linebuffer[currentline]^ := emptyline^;
  707.  
  708.    linebuffer[currentline] := emptyline;
  709.  end;
  710.  
  711.  
  712.  procedure locate;
  713.  var
  714.    temp:               linestring;
  715.    pointer,
  716.    position,
  717.    line,
  718.    len:                integer;
  719.    
  720.  begin
  721.    window(1, 1, 80, 25);
  722.    gotoxy(1, 25);
  723.    clreol;
  724.    write('Locate:     Enter string: <',searchstring,'> ');
  725.    temp := '';
  726.    read(temp);
  727.    if temp <> '' then
  728.       searchstring := temp;
  729.    len := length (searchstring);
  730.  
  731.    if len = 0 then
  732.    begin
  733.       displaykeys;
  734.       beginfile;
  735.       exit;
  736.    end;
  737.  
  738.    gotoxy(1, 25);
  739.    clreol;
  740.    write('Searching...  Press <ESC> to exit, <HOLD> to pause');
  741.    window(1, 2, 80, 23);
  742.    clrscr;
  743.  
  744.    for i := 1 to highestline do
  745.    begin
  746.    (* look for matches on this line *)
  747.       pointer := pos (searchstring, linebuffer [i]^);
  748.  
  749.     (* if there was a match then get ready to print it *)
  750.       if (pointer > 0) then
  751.       begin
  752.          temp := linebuffer [i]^;
  753.          position := pointer;
  754.          gotoxy(1, wherey);
  755.          lowvideo;
  756.          write(copy(temp,1,79));
  757.          normvideo;
  758.  
  759.          (* print all of the matches on this line *)
  760.          while pointer > 0 do
  761.          begin
  762.             gotoxy(position, wherey);
  763.             write(copy (temp, pointer, len));
  764.             temp := copy (temp, pointer + len + 1, 128);
  765.             pointer := pos (searchstring, temp);
  766.             position := position + pointer + len;
  767.          end;
  768.  
  769.          (* go to next line and keep searching *)
  770.          writeln;
  771.       end;
  772.    end;
  773.  
  774.    window(1, 1, 80, 25);
  775.    gotoxy(1, 25);
  776.    clreol;
  777.    write('End of locate.  Press any key to exit...');
  778.  
  779.    repeat
  780.    until keypressed;
  781.    read(kbd,ch);
  782.  
  783.    displaykeys;
  784.    beginfile;
  785.  end;
  786.  
  787.  
  788.  procedure search;
  789.  var
  790.    temp:               linestring;
  791.    pointer,
  792.    position,
  793.    line,
  794.    len:                integer;
  795.  
  796.  begin
  797.    window(1, 1, 80, 25);
  798.    gotoxy(1, 25);
  799.    clreol;
  800.    write('Search:     Enter string: <',searchstring,'> ');
  801.    temp := '';
  802.    read(temp);
  803.    if temp <> '' then
  804.       searchstring := temp;
  805.    len := length (searchstring);
  806.  
  807.    if len = 0 then
  808.    begin
  809.       displaykeys;
  810.       beginfile;
  811.       exit;
  812.    end;
  813.  
  814.    gotoxy(1, 25);
  815.    clreol;
  816.    write('Searching...');
  817.    window(1, 2, 80, 23);
  818.  
  819.    for i := currentline+1 to highestline do
  820.    begin
  821.    (* look for matches on this line *)
  822.       pointer := pos (searchstring, linebuffer [i]^);
  823.  
  824.     (* if there was a match then get ready to print it *)
  825.       if (pointer > 0) then
  826.       begin
  827.          currentline := i;
  828.          if currentline >= 12 then
  829.             screenline := 12
  830.          else
  831.             screenline := currentline;
  832.  
  833.          drawscreen;
  834.          column := pointer;
  835.          displaykeys;
  836.          exit;
  837.       end;
  838.    end;
  839.  
  840.    window(1, 1, 80, 25);
  841.    gotoxy(1, 25);
  842.    clreol;
  843.    write('Search string not found.  Press any key to exit...');
  844.  
  845.    repeat
  846.    until keypressed;
  847.    read(kbd,ch);
  848.  
  849.    displaykeys;
  850.  end;
  851.  
  852.  procedure replace;
  853.  var
  854.    temp:               linestring;
  855.    position,
  856.    line,
  857.    len:                integer;
  858.  
  859.  begin
  860.    window(1, 1, 80, 25);
  861.    gotoxy(1, 25);
  862.    clreol;
  863.    write('Replace:     Enter search string: <',searchstring,'> ');
  864.    temp := '';
  865.    read(temp);
  866.    if temp <> '' then
  867.       searchstring := temp;
  868.  
  869.    len := length (searchstring);
  870.    if len = 0 then
  871.    begin
  872.       displaykeys;
  873.       exit;
  874.    end;
  875.  
  876.    gotoxy(1, 25);
  877.    clreol;
  878.    write('Replace:     Enter replacement string: <',replacement,'> ');
  879.    temp := '';
  880.    read(temp);
  881.    if temp <> '' then
  882.       replacement := temp;
  883.    len := length (replacement);
  884.  
  885.    gotoxy(1, 25);
  886.    clreol;
  887.    write('Searching...');
  888.    window(1, 2, 80, 23);
  889.    clrscr;
  890.  
  891.    for line := 1 to highestline do
  892.    begin
  893.       position := pos (searchstring, linebuffer [line]^);
  894.  
  895.       while (position > 0) do
  896.       begin
  897.          currentline := line;
  898.          if currentline >= 12 then
  899.             screenline := 12
  900.          else
  901.             screenline := currentline;
  902.  
  903.          drawscreen;
  904.          column := position;
  905.          lowvideo;
  906.          gotoxy(column,screenline);
  907.          write(column,screenline,searchstring);
  908.          normvideo;
  909.  
  910.          window(1, 1, 80, 25);
  911.          gotoxy(1, 25);
  912.          clreol;
  913.          write('Replace (Y/N/ESC)? ');
  914.          read(kbd, choice);
  915.  
  916.          if ord (choice)= 27 then
  917.          begin
  918.             displaykeys;
  919.             beginfile;
  920.             exit;
  921.          end;
  922.  
  923.          gotoxy(1, 25);
  924.          clreol;
  925.          write('Searching...');
  926.          window(1, 2, 80, 23);
  927.          gotoxy(1,line);
  928.  
  929.  
  930.          if choice in ['y','Y'] then
  931.          begin
  932.             linebuffer[line]^ := copy (linebuffer [line]^, 1, position - 1) +
  933.                                    replacement +
  934.                                    copy (linebuffer [line]^, position +
  935.                                            length (searchstring), 128);
  936.  
  937.             position := pos (searchstring, copy (linebuffer[line]^,
  938.                                 position + len + 1,128)) +
  939.                             position + len;
  940.          end
  941.          else
  942.             position := pos (searchstring, copy (linebuffer[line]^,
  943.                                position + length(searchstring) + 1,128)) +
  944.                           position + length(searchstring);
  945.  
  946.          gotoxy(1,screenline);
  947.          clreol;
  948.          write(copy(linebuffer[currentline]^,1,79));
  949.       end;
  950.    end;
  951.  
  952.    window(1, 1, 80, 25);
  953.    gotoxy(1, 25);
  954.    clreol;
  955.    write('End of replace.  Press any key to exit...');
  956.  
  957.    repeat
  958.    until keypressed;
  959.    read(kbd,ch);
  960.  
  961.    displaykeys;
  962.  end;
  963.  
  964.  
  965.  
  966.  
  967.  procedure handlefunc;
  968.  begin
  969.  
  970.    case keynum of
  971.       8:  backspace;
  972.       9:  tab;
  973.      13:  enter;
  974.      27:  esc;
  975.     143:  backtab;
  976.     187:  help;
  977.     188:  locate;
  978.     189:  search;
  979.     190:  replace;
  980.     191:  terminate;
  981.     192:  insertline;
  982.     193:  deleteline;
  983.     196:  quitnosave;
  984.     199:  column := 1;
  985.     200:  cursorup;
  986.     201:  funcpgup;
  987.     203:  cursorleft;
  988.     205:  cursorright;
  989.     207:  funcend;
  990.     208:  cursordown;
  991.     210:  ins;
  992.     211:  del;
  993.     209:  funcpgdn;
  994.     243:  prevword;
  995.     244:  nextword;
  996.     246:  endfile;
  997.     260:  beginfile;
  998.  
  999.     else  begin
  1000. {             write('[',keynum:3,']'^H^H^H^H^H);}
  1001.              sound(200);
  1002.              delay(300);
  1003.              nosound;
  1004.           end;
  1005.    end;
  1006.  end;
  1007.  
  1008.  
  1009. (* main *)
  1010.  
  1011.  begin
  1012.    initialize;
  1013.    printrow;
  1014.  
  1015.  
  1016. (* main loop - get a key and process it *)
  1017.  
  1018.    repeat
  1019.       gotoxy(column, screenline);
  1020.  
  1021.  
  1022.       secnum := false;
  1023.       if getkey (secnum, inkey) then
  1024.       begin
  1025.  
  1026.          if secnum then
  1027.             handlefunc
  1028.          else
  1029.             character;
  1030.  
  1031.          printrow;
  1032.       end;
  1033.  
  1034.    until true = false;
  1035.  
  1036.  end.
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.